package com.yanghs.shiro.realm;

import com.yanghs.common.entity.hbm.*;
import com.yanghs.common.service.IRoleService;
import com.yanghs.common.service.IUserService;
import com.yanghs.shiro.exception.CustomAuthenticationException;
import com.yanghs.shiro.token.UserToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author yanghs
 * @Description:自定义数据获取
 * @date 2018/3/1 13:57
 */
public class UserRealm extends AuthorizingRealm {
    @Resource(name = "userService")
    IUserService userService;
    @Resource(name = "roleService")
    IRoleService roleService;
    /**
     * 授权逻辑获取
     * @param principalCollection
     * @return
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Userinfo userinfo = (Userinfo) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo sa = new SimpleAuthorizationInfo();
        try {
            Collection<Authority> authorityCollection = roleService.getRoleByUser(userinfo);
            Iterator<Authority> authorityIterator = authorityCollection.iterator();
            while (authorityIterator.hasNext()){
                Authority authority = authorityIterator.next();
                sa.addStringPermission(authority.getUrl());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sa;
    }

    /**
     * 用户登录逻辑实现
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UserToken token = (UserToken) authenticationToken;
        Userinfo userInfo = null;
        try {
            userInfo =  userService.getUser(new Userinfo(null,token.getUsername(),null,null));
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomAuthenticationException("查询数据出错请联系管理员"+e.getMessage());
        }
        if(userInfo != null){
            if(userInfo.getUsable().equals("00")){
                throw  new CustomAuthenticationException("账号已停用");
            }else if(userInfo.getLocked().equals("01")){
                throw  new CustomAuthenticationException("账号已被锁定");
            }else {
                AuthenticationInfo authcinfo = new SimpleAuthenticationInfo(userInfo,userInfo.getPassword(), ByteSource.Util.bytes("-lab"),this.getName());
                return  authcinfo;
            }
        }
        return null;
    }

    /**
     * 清除认证缓存
     */
    public void clearCachedAuthenticationInfo() {
        super.clearCachedAuthenticationInfo(SecurityUtils.getSubject().getPrincipals());
    }

    /**
     * 清除权限缓存
     */
    public void clearCachedAuthorizationInfo() {
        super.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
    }
}
